package com.road.utils;

import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

public class VerifyCome {
	
	static String[] deviceArray = new String[]{"android","mac os","windows phone","micromessenger","yyb_d qq","vivobrowser","qzone","baiduboxapp","wkbrowser","dingtalk","(ap/","samsungbrowser","weibo","windowswechat"};
	
	public static String  isMobileDevice(HttpServletRequest request){
//		SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//		//System.out.println(request.getSession().getId()+"session有效期："+request.getSession().getMaxInactiveInterval());
//		//System.out.println("创建时间："+sm.format(request.getSession().getCreationTime())+"访问时间："+sm.format(request.getSession().getLastAccessedTime()));
//		request.getSession().setMaxInactiveInterval(-1);
//		
		Enumeration<String> namse = request.getHeaderNames();
		while (namse.hasMoreElements()) {
			String string = (String) namse.nextElement();
			System.out.println("HeaderName参数:"+string+" ；值："+request.getHeader(string));
		}
		
		Enumeration<String> params = request.getParameterNames();
		while (params.hasMoreElements()) {
			String string = (String) params.nextElement();
			System.out.println("ParameterName参数:"+string+" ；值："+request.getParameter(string));
		}
		
        /**
         * android : 所有android设备
         * mac os : iphone ipad
         * windows phone:Nokia等windows系统的手机
         */
		String custom="";
		
		String requestHeader=request.getHeader("user-agent").toLowerCase();
//		System.out.println(requestHeader);
        if(requestHeader.indexOf(deviceArray[0])>0) {
        	custom="android 设备》》";
        } 
        else  if(requestHeader.indexOf(deviceArray[1])>0) { 
        	custom="apple 设备》》";
        }
        else if(requestHeader.indexOf(deviceArray[2])>0) { 
        	custom="windows phone 设备》》";
        }
      
        Map<String, Object> map = getPhoneDevice(requestHeader,request);
        boolean istrue=(Boolean) map.get("value");
        return custom+map.get("tips")+(istrue==true?"有效":"无效");
   }
	
	public static Map<String, Object> getPhoneDevice(String requestHeader,HttpServletRequest request){
		String result="";
		boolean value=false;
		Map<String, Object> map=new HashMap<String, Object>();
		if(requestHeader.indexOf(deviceArray[3])>0){
        		if(null!=request.getParameter("from")) {
        			if(request.getParameter("from").equals("timeline")) {
        				if(requestHeader.indexOf(deviceArray[13])>0) {
        					result="来自 微信PC端朋友圈的分享投票链接";
        				}else {
        					result="来自 微信手机端朋友圈的分享投票链接";
        				}
        				
        			}else if(request.getParameter("from").equals("groupmessage")){
        				if(requestHeader.indexOf(deviceArray[13])>0) {
        					result="来自 微信PC端群的分享投票";
        				}else {
        					result="来自 微信手机端群的分享投票";
        					value=true;
        				}
        				
        			}else if(request.getParameter("from").equals("singlemessage")){
        				if(requestHeader.indexOf(deviceArray[13])>0) {
        					result="来自 微信PC端群个人的分享投票";
        				}else {
        					result="来自 微信手机端群个人的分享投票";
        				}
        			}
        		}
        		else if(null!=request.getParameter("winzoom")){
        			if(request.getParameter("winzoom").equals("1")) {
        				result="来自 微信电脑端浏览器客户端直接访问";
        			}
				}else {
					
					if(request.getHeader("referer").contains("from=groupmessage")) {
						result="来自 微信手机端群的分享投票";
    					value=true;
					}else {
						result="来自 微信连接直接访问";
					}
					
				}
        	
        }else if(requestHeader.indexOf(deviceArray[4])>0){
        	result="来自 qq客户端访问";
        }else if(requestHeader.indexOf(deviceArray[5])>0){
        	result="来自 vivo浏览器客户端";
        }else if(requestHeader.indexOf(deviceArray[6])>0){
        	result="来自 qq空间客户端";
        }else if(requestHeader.indexOf(deviceArray[7])>0) {
        	result="来自 百度客户端";
        }else if(requestHeader.indexOf(deviceArray[8])>0) {
        	result="来自 万能钥匙";
        }else if(requestHeader.indexOf(deviceArray[9])>0) {
        	result="来自 钉钉";
        }else if(requestHeader.indexOf(deviceArray[10])>0) {
        	result="来自 支付宝";
        }else if(requestHeader.indexOf(deviceArray[11])>0) {
        	result="来自 三星浏览器";
        }else if(requestHeader.indexOf(deviceArray[12])>0) {
        	result="来自 新浪微博客户端";
        }else {
        	result="来自  PC端浏览器";
		}
		map.put("value", value);
		map.put("tips", result);
		return map;
	}
	
	static String[] deviceFormArray = new String[]{"android","mac os","windows phone","micromessenger"};
	public static final String KEY_FORM="from";
	public static final String KEY_OK="ok";
	public static Map<String, Object> fromOurApp(HttpServletRequest request){
     String custom="";
     boolean ok = false;
		
		String requestHeader=request.getHeader("user-agent").toLowerCase();
//		System.out.println(requestHeader);
        if(requestHeader.indexOf(deviceArray[0])>0&&
        		requestHeader.indexOf(deviceArray[3])>0) {
        	custom="android 设备》》";
        	ok=true;
        } 
        else  if(requestHeader.indexOf(deviceArray[1])>0&&
        		requestHeader.indexOf(deviceArray[3])>0) { 
        	custom="apple 设备》》";
        	ok=true;
        }
        else if(requestHeader.indexOf(deviceArray[2])>0&&
        		requestHeader.indexOf(deviceArray[3])>0) { 
        	custom="windows phone 设备》》";
        	ok=true;
        	
        }else {
        	custom="非法的访问来源";
        }
		Map<String, Object> map=new HashMap<>();
		map.put(KEY_FORM, custom);
		map.put(KEY_OK, ok);
		return map;
	}
}
